package org.easyexcel.MydoMian;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openjdk.jcstress.annotations.Actor;
import org.openjdk.jcstress.annotations.Arbiter;
import org.openjdk.jcstress.annotations.Expect;
import org.openjdk.jcstress.annotations.JCStressTest;
import org.openjdk.jcstress.annotations.Outcome;
import org.openjdk.jcstress.annotations.State;
import org.openjdk.jcstress.infra.results.II_Result;

import java.io.IOException;

/**
 * @author DPF
 */
public class jsonJ {
    public static void main(String[] args) {
        new MyServiceJsonTest().run();
    }

}
interface MyService {
    String getData(String param);
}
class MyServiceImpl implements MyService {
    @Override
    public String getData(String param) {
        if ("param1".equals(param)) {
            return "{\"key\": \"value1\"}";
        } else if ("param2".equals(param)) {
            return "{\"key\": \"value2\"}";
        } else {
            return "{}";
        }
    }
}
@JCStressTest
@Outcome(id = "{\"key\": \"value1\", \"key\": \"value2\"", expect = Expect.ACCEPTABLE, desc = "Both actors got the expected JSON data")
@State
class MyServiceJsonTest {

    private final MyService service = new MyServiceImpl();
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Actor
    public void actor1(II_Result result) throws IOException {
        String json = service.getData("param1");
        JsonNode node = objectMapper.readTree(json);
        result.r1 = Integer.parseInt(node.toString());
    }

    @Actor
    public void actor2(II_Result result) throws IOException {
        String json = service.getData("param2");
        JsonNode node = objectMapper.readTree(json);
        result.r2 = Integer.parseInt(node.toString());
    }

    @Arbiter
    public void arbiter(II_Result result) {
        // 验证结果是否符合预期
        if (result.r1.equals("{\"key\":\"value1\"}") && result.r2.equals("{\"key\":\"value2\"}")) {
            // 结果符合预期
        } else {
            throw new AssertionError("Unexpected result: " + result);
        }
    }
}

